import { ref, reactive } from 'vue'
import { message } from 'ant-design-vue'
import Api from '@/api/org.js'

export const useDepartmentForm = () => {
  const formRef = ref()
  const formState = reactive<any>({
    realName: '',
    password: '',
    passwordAgain: '',
    organization: undefined,
    department: '',
    company: '',
  })

  const realNameValid = async (_rule: any, value: string) => {
    if (!value) {
      return Promise.reject('请输入真实姓名')
    }
    return Promise.resolve()
  }

  const passwordValid = async (_rule: any, value: string) => {
    if (!value) {
      return Promise.reject('请输入密码')
    }
    if (value.length < 6) {
      return Promise.reject('密码长度不能少于6位')
    }
    return Promise.resolve()
  }

  const passwordAgainValid = async (_rule: any, value: string) => {
    console.log(value, formState.password)

    if (value !== formState.password) {
      return Promise.reject('两次输入的密码不一致')
    }
    return Promise.resolve()
  }

  const organizationValid = async (_rule: any, value: string) => {
    if (!value) {
      return Promise.reject('请输入所属组织')
    }
    return Promise.resolve()
  }

  const rules = {
    realName: [
      {
        required: true,
        validator: realNameValid,
        trigger: 'change',
      },
      {
        max: 20,
        message: '真实姓名不能超过20个字符',
        trigger: 'blur',
      },
    ],
    password: [
      {
        required: true,
        validator: passwordValid,
        trigger: 'change',
      },
    ],
    passwordAgain: [
      {
        required: true,
        validator: passwordAgainValid,
        trigger: 'change',
      },
    ],
    organization: [
      {
        required: true,
        validator: organizationValid,
        trigger: 'change',
      },
      {
        max: 20,
        message: '所属组织不能超过20个字符',
        trigger: 'blur',
      },
    ],
    department: [
      {
        max: 20,
        message: '所属部门不能超过20个字符',
        trigger: 'blur',
      },
    ],
    company: [
      {
        max: 20,
        message: '所属单位不能超过20个字符',
        trigger: 'blur',
      },
    ],
  }

  const resetForm = () => {
    formRef.value?.resetFields()
    Object.assign(formState, {
      realName: '',
      password: '',
      passwordAgain: '',
      organization: '',
      department: '',
      company: '',
    })
    console.log(formState);
    
  }

  const submitForm = async () => {
    console.log(formState);
    
    try {
      // await formRef.value.validate()
      const params = {
        realName: formState.realName,
        password: formState.password,
        organization: formState.organization,
        department: formState.department,
        company: formState.company,
      }

      console.log(params)

      /* if (formState.id) {
        await Api.update(params)
        message.success('修改成功')
      } else {
        await Api.add(params)
        message.success('新增成功')
      } */
      return true
    } catch (error) {
      console.error('表单提交错误:', error)
      return false
    }
  }

  return {
    formRef,
    formState,
    rules,
    resetForm,
    submitForm,
  }
}
